---
title: "Figure 1"
author: "Yingjie Fan"
date: "2023-06-01"
---

```{r setup, include=FALSE}
rm(list=ls())

#install.packages(c("dplyr", "data.table", "tidyr"))
library(dplyr)
library(data.table)
library(tidyr)
library(ggplot2)
path = "" # Insert path
```

Data Source: 

All Follower count data is directly retrieved from https://socialblade.com. To access, register for its business API credits to retrieve all the historical daily follower count for each Twitter account. After registertion, obtain a clientid and token (marked with xxx below) to complete the following sample query and download the raw data.

Sample query: https://matrix.sbapis.com/b/twitter/statistics?query=ChinaDaily&clientid=xxx&token=xxx&history=vault 

After retrieving available historical data from Social Blade, we pre-processed the data as follows:

```{r Pre-processing Raw data}
### Read and pre-processing the follower count data from social blade
# follower_d = fread(paste0(path,"/Data/Followers/follower_social_blade.csv"), drop = 1)%>%
#   mutate(followers = round(followers/1000000,2))
# write.csv(follower_d,paste0(path,"/Data/Followers/follower.csv"))
```


```{r Figure 1: Followership}
### Generate the figures for follower count data for Chinese outlets
follow_china<-fread(paste0(path,"/Data/Followers/follower.csv")) %>%
  filter(username %in% c("CGTN","China Daily","Xinhua","People\'s Daily"))%>%
 ggplot(aes(x=as.Date(date), y=followers,linetype=username)) +  
 geom_line()+
  theme_bw()+
  expand_limits(x=as.Date("2020-08-01"))+
   scale_x_date(date_breaks = 'year', date_labels = '%Y')+
  geom_text(aes(label=ifelse(date=="2020-01-10",as.character(username),'')),hjust=0,vjust=-0.5)+
  labs(y="Followership (in Millions)", x = "",title="")+ 
  theme(legend.position = "none",
        legend.title = element_blank(),
        legend.text=element_text(size=12), axis.text.x = element_text(size=12),
        axis.text.y = element_text(size=12), axis.title=element_text(size=12))+
  scale_linetype_manual(values = c("CGTN" = "solid", "Xinhua" = "dotted","China Daily" = "longdash","People\'s Daily" = "dotdash")) +
  guides(linetype = "none")

### Save Figure 1 output
ggsave(paste0(path, "/Output/figure1_follower.png"),follow_china,width=7, height=5)
```

